package com.putable.nmc.dmv.tests;

import java.io.StringWriter;
import java.io.Writer;

import junit.framework.TestCase;

import com.putable.nmc.dmv.BadTeller;
import com.putable.nmc.dmv.Customer;
import com.putable.nmc.dmv.CustomerLine;
import com.putable.nmc.dmv.Door;
import com.putable.nmc.dmv.PQElement;
import com.putable.nmc.world.CIT;
import com.putable.nmc.world.World;
import com.putable.pqueue.PQueue;
import com.putable.pqueue.PQueueImpl;

public class TestBadTeller extends TestCase {
	
	public void testBadTellerConstructor(){
		PQueue pq = new PQueueImpl();
		BadTeller teller = new BadTeller(pq);
		assertEquals(teller, pq.remove());
	}
	
	public void testServeOnce(){
		PQueue pq = new PQueueImpl();
		CustomerLine cLine = new CustomerLine();
		new BadTeller(pq);
		World world  = new World();
		double time = 0;
		Door door = new Door(pq);
		
		CIT cit = world.getNextCIT(time);
		Customer c = new Customer(pq, cit, world);
		door.addCustToPQ(time, c);
		
		for(int i = 0; i<4; i++){
			PQElement pqe = (PQElement) pq.remove();
			time = pqe.getTimeOfNextTask();
			pqe.run(time, cLine);
		}
		
		Writer writer = new StringWriter();
		assertTrue(cit.getEntered());
		assertFalse(world.getScore(writer));
		System.out.println(writer.toString());
		
	}
	
	public void testServeMult(){
		PQueue pq = new PQueueImpl();
		CustomerLine cLine = new CustomerLine();
		for(int i = 0; i < 8; i ++){
			new BadTeller(pq);
		}
		World world  = new World();
		double time = 0;
		Door door = new Door(pq);
		int counter = 0;
		while(true){
			CIT cit = world.getNextCIT(time);
			Customer c = new Customer(pq, cit, world);
			if(cit.getGiveupTime() < 36000){
				counter++;
			}
			door.addCustToPQ(time, c);
		
			PQElement pqe = (PQElement) pq.remove();
			time = pqe.getTimeOfNextTask();
			pqe.run(time, cLine);
			if(time > 36000) break;

		}

		Writer writer = new StringWriter();
		assertFalse(world.getScore(writer));
		System.out.println(writer.toString());
		System.out.println(counter);
	}

}
